﻿
using System.Windows;
using System.Windows.Input;

namespace Labthink.PCSoft.UniversalHost4.UserControls.CustomControls
{
    public class CustomChromeWindow : Window
    {

        #region DependencyProperty


        public double CaptionHeight
        {
            get => (double)GetValue(CaptionHeightProperty);
            set => SetValue(CaptionHeightProperty, value);
        }

        // Using a DependencyProperty as the backing store for CaptionHeight.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty CaptionHeightProperty =
            DependencyProperty.Register("CaptionHeight", typeof(double), typeof(CustomChromeWindow), new UIPropertyMetadata(23d));


        public CornerRadius CornerRadius
        {
            get => (CornerRadius)GetValue(CornerRadiusProperty);
            set => SetValue(CornerRadiusProperty, value);
        }

        // Using a DependencyProperty as the backing store for CornerRadius.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty CornerRadiusProperty =
            DependencyProperty.Register("CornerRadius", typeof(CornerRadius), typeof(CustomChromeWindow), new UIPropertyMetadata(default(CornerRadius)));



        #endregion


        /// <summary>
        /// Initializes the metadata for the window
        /// </summary>
        static CustomChromeWindow()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomChromeWindow),
                new FrameworkPropertyMetadata(typeof(CustomChromeWindow)));
        }

        public CustomChromeWindow()
        {
            // 显示系统菜单项
            CommandBinding showSystemMenuCmdBinding = new CommandBinding(
                Microsoft.Windows.Shell.SystemCommands.ShowSystemMenuCommand,
                _OnShowSystemMenuCommand
            );
            // 关闭窗体命令
            CommandBinding closeWindowCmdBinding = new CommandBinding(
                Microsoft.Windows.Shell.SystemCommands.CloseWindowCommand,
                _OnCloseWindowCommand
            );

            // 绑定路由命令
            CommandBindings.Add(showSystemMenuCmdBinding);
            CommandBindings.Add(closeWindowCmdBinding);


        }

        private void _OnCloseWindowCommand(object sender, ExecutedRoutedEventArgs e)
        {
            Window window = GetWindow(this);

            Microsoft.Windows.Shell.SystemCommands.CloseWindow(window);
        }

        private void _OnShowSystemMenuCommand(object sender, ExecutedRoutedEventArgs e)
        {
            Window window = GetWindow(this);
            if (window != null)
            {
                Point point = new Point(window.Left + 24, window.Top + 24);
                Microsoft.Windows.Shell.SystemCommands.ShowSystemMenu(window, point);
            }
        }

    }
}
